<template>
  <div class="games-related">
    <ul>
      <li id="'game_'+ item.gameId" v-for="(item, index) in gameList">
        <span v-if="item.gameStage =='fighting'">TA正在参加&nbsp;&nbsp;</span>
        <span v-if="item.gameStage =='signingUp'|| item.gameStage =='missed'">TA报名参加了&nbsp;&nbsp;</span>
        <a href="javascript;" @click.stop.prevent="toGame(item)">{{item.gameName}}</a>
        <span class="pull-right" v-if="item.gameStage =='fighting'">
                <span>&nbsp;&nbsp;当前排名:</span>
                <span class="rankNum">{{item.ranknum <=0 ? '--' : item.ranknum}}</span>
        </span>
        <span class="pull-right" v-if="item.gameStage =='signingUp'|| item.gameStage =='missed'">报名中</span>
      </li>
      <li v-if="gameList.length==0">
        <span>TA当前没有正在参加的比赛</span>
      </li>
    </ul>
  </div>
</template>

<script>
  import RemoteDAO from 'common/remoteDAO'
  import CommonUtil from 'common/common.util'
  import LocalDAO from 'common/localDAO'
  import Constant from 'common/constant'

  export default {
    data () {
      return {
        gameList: []
      }
    },
    props: {
      clientId: {
        type: String
      }
    },
    created: function () {
      this.fetchData()
    },
    watch: {
      '$route.query.userCode' (to, from) {
        //跳转相同页面，组件不重新构建，无法调用created,故需监听
        this.fetchData()
      }
    },
    methods: {
      fetchData(){
        var self = this;
        //查询比赛列表
        let params = {
          userCode: self.clientId,
          actionFlag: '0',
          pageSize: '500',
          pageNo: '1',
          totalFlag: '0',
          gameKind: '',
          gameID: ''
        }
        RemoteDAO.callUC12008303(params, function (response) {
          var list = [];
          var gameIds = [];
          if (Number(response.error) === 0) {
            _.each(response.data.body, function (item, index) {
              var object = {};

              object.gameId = item.GAME_ID;
              object.gameName = item.GAME_NAME;
              object.upGameId = item.UP_GAME_ID;
              object.upGameName = item.UP_GAME_NAME;
              object.gameType = item.GAME_TYPE;
              object.begainDate = item.BEGIN_DATE;
              object.endDate = item.END_DATE;
              object.joinBeginDate = item.JOIN_BEGIN_DATE;
              object.joinEndDate = item.JOIN_END_DATE;
              object.gameMaxLimit = item.GAME_MAX_LIMIT;
              object.gameAttendPerson = item.GAME_ATTEND_PERSON;
              object.createUser = item.CREATE_USER;
              object.gameStatus = item.GAME_STATUS;
              if (object.gameName.length > 8) {
                object.gameName = object.gameName.slice(0, 8) + "...";
              }

              switch (item.GAME_STEP) {
                case "0": // 未开始比赛
                  object.gameStage = Constant.game.stage.NOT_OPEN;
                  break;
                case "1": // 报名中
                  object.gameStage = Constant.game.stage.SIGNING_UP;
                  break;
                case "2": // 报名结束
                  object.gameStage = Constant.game.stage.MISSED;
                  break;
                case "3": // 比赛中
                  object.gameStage = Constant.game.stage.FIGHTING;
                  break;
                case "4": // 比赛结束
                  object.gameStage = Constant.game.stage.OVER;
                  break;
                case "5": // 比赛无效
                  object.gameStage = Constant.game.stage.INVALID;
                  break;
              }
              if (object.gameStatus == "1" && object.gameStage == Constant.game.stage.FIGHTING) {
                gameIds.push(item.GAME_ID);
              }

              // 只展示审核通过，且报名中、报名已结束、比赛中的比赛
              if (object.gameStatus == "1" && (object.gameStage == Constant.game.stage.SIGNING_UP || object.gameStage == Constant.game.stage.MISSED ||
                object.gameStage == Constant.game.stage.FIGHTING)) {
                list.push(object);
              }
            });
          } else {
            self.$store.dispatch('setTip', {text: "UC12008303:"+response.data});
          }

          var args = {};
          args.game_id = gameIds.join(",");
          args.client_id = self.clientId;
          if (gameIds.length > 0) {
            //查询我参加的多个比赛资产数据
            RemoteDAO.callJCL30028(args, function (response) {
              if (response.error == "0") {
                _.each(response.data.body.data, function (item, index) {
                  _.each(list, function (itemTemp) {
                    if (itemTemp.gameId == item.game_id) {
                      itemTemp.ranknum = item.ranknum;
                    }
                  });
                });
              } else {
                self.$store.dispatch('setTip', {text: "JCL30028:"+response.data});
              }
              self.gameList = list;

              // 更新分享页数据
              var shareOtherDetails = LocalDAO.getShareOtherDetails();
              shareOtherDetails.gamesRelated = {list:self.gameList};
              LocalDAO.setShareOtherDetails(shareOtherDetails);
            });
          }
        });
      },
      toGame(item){
        if (item.gameType === 'TDS') {
          this.$router.push({ path: '/team-game/team-detail', query: { gameId: item.gameId, gameTitle: item.upGameName }});
        } else {
          this.$router.push({path: '/game/gamedetail', query: {gameId: item.gameId}});
        }
      }
    }
  }
</script>

<style scoped>

</style>
